package com.cyandpos.pad.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.changyi.device.APApplication;
import com.changyi.device.printer.PrintManagerLib;
import com.cyandpos.pad.R;
import com.cyandpos.pad.common.ticketformat.TicketFormat;
import com.cyandpos.pad.login.configs.GetConfigs;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.TicketInfo;
import com.cyandpos.pad.reprintticket.IReprintView;
import com.cyandpos.pad.reprintticket.reprintPresenter.IReprintPresenter;
import com.cyandpos.pad.reprintticket.reprintPresenter.ReprintPresenterImpl;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class ReprintTicketActivity extends AppCompatActivity implements IReprintView, View.OnClickListener {
    /**
     * 原记录编号
     */
    private EditText ed_old_saleId;
    private IReprintPresenter reprintTicketPresent;
    private Button btn_ensure_reprintTicket;
    private Toolbar toolbar;
    private TextView tv_dialogtitle_name;

    private ProgressDialog dialog;
    private Spinner sp_repType;
    private int reFlag = 0;//0 交易小票  1 微信小票    2支付宝小票  3本地储值卡
    private SelfNumKeyView sf_containt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reprint);
        APApplication.getInstance().addActivity(this);
        reprintTicketPresent = new ReprintPresenterImpl(this);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {

        ed_old_saleId = (EditText) findViewById(R.id.ed_old_saleId);
        btn_ensure_reprintTicket = (Button) findViewById(R.id.btn_ensure_reprintTicket);
        sp_repType = (Spinner) findViewById(R.id.sp_printType);
        tv_dialogtitle_name = (TextView) findViewById(R.id.tv_dialog_title_name);
        sf_containt = (SelfNumKeyView) findViewById(R.id.member_soft_keyboard);

        sf_containt.setEnterReceptor(ed_old_saleId);
        sf_containt.setIsCanInputPoint(true);
        sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
            @Override
            public void keyBoardSure(String num,String hint) {
              //  ed_old_saleId.setText(num);
               // mRefundPresenter.getOldTicket(isOrderNum);

            }
        });

        CommonUtils.setNoSoftware(ed_old_saleId, ReprintTicketActivity.this, true);

        ed_old_saleId.setHint("原交易订单号/交易号");
        //数据
        List<String> data_list = new ArrayList<String>();
        data_list.add("交易小票");
//        data_list.add("订金单小票");
        data_list.add("微信小票");
        data_list.add("支付宝小票");
        //适配器
        final ArrayAdapter<String> arr_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        sp_repType.setAdapter(arr_adapter);
        //设置默认值
        sp_repType.setSelection(0);//.setOnItemSelectedListener

        sp_repType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                String repTypes = arr_adapter.getItem(position);
                TextView tv = (TextView) view;
                tv.setTextColor(Color.BLACK);
                if (repTypes.equals("交易小票")) {
                    reFlag = 0;
                    //获取本地微信流水号上一笔
                    SharedPreferences WXpref = getSharedPreferences("TradeNo", Context.MODE_PRIVATE);
                    String wxTransactionId = WXpref.getString("TicketOrderNo", "");
                    ed_old_saleId.setHint("原交易订单号/交易号");
                    if (!wxTransactionId.equals(""))
                        ed_old_saleId.setText(wxTransactionId);

                } else if (repTypes.equals("微信小票")) {
                    reFlag = 1;
                    //获取本地微信流水号上一笔
                    SharedPreferences WXpref = getSharedPreferences("WXID", Context.MODE_PRIVATE);
                    String wxTransactionId = WXpref.getString("WXTransactionId", "");
                    ed_old_saleId.setHint("原微信交易流水号");
                    if (!wxTransactionId.equals(""))
                        ed_old_saleId.setText(wxTransactionId);
                } else if (repTypes.equals("支付宝小票")) {
                    reFlag = 2;
                    //获取本地支付宝流水号上一笔
                    SharedPreferences ZFBpref = getSharedPreferences("ZFBID", Context.MODE_PRIVATE);
                    String zfbTransactionId = ZFBpref.getString("ZFBTransactionId", "");
                    ed_old_saleId.setHint("原支付宝交易流水号");
                    if (!zfbTransactionId.equals(""))
                        ed_old_saleId.setText(zfbTransactionId);

                } else if (repTypes.equals("本地储值卡小票")) {
                    reFlag = 3;
//                    ed_old_saleId.setText((Integer.valueOf(ErpCommonData.posTradeNum) - 1) + "");
                } else if (repTypes.equals("异地卡消费小票")) {
                    reFlag = 4;
                    //获取本地异地储值卡CRM交易号上一笔
                    SharedPreferences Remotepref = getSharedPreferences("RemoteID", Context.MODE_PRIVATE);
                    String remoteTraceId = Remotepref.getString("RemoteCashCardTraceId", "");
                    ed_old_saleId.setHint("原异地卡消费CRM交易号");
                    if (!remoteTraceId.equals(""))
                        ed_old_saleId.setText(remoteTraceId);
                } else if (repTypes.equals("异地卡撤销小票")) {
                    reFlag = 5;
                    //获取本地异地储值卡撤销CRM交易号上一笔
                    SharedPreferences Remotepref = getSharedPreferences("RemoteCancleID", Context.MODE_PRIVATE);
                    String remoteCancleTraceId = Remotepref.getString("RemoteCashCardCancleTraceId", "");
                    if (remoteCancleTraceId.equals(""))
                        ed_old_saleId.setHint("原异地卡撤销CRM交易号");
                    else
                        ed_old_saleId.setText(remoteCancleTraceId);
                }
                else if (repTypes.equals("订金单小票")) {
                    reFlag = 6;
                    //获取本地异地储值卡撤销CRM交易号上一笔
                    SharedPreferences Remotepref = getSharedPreferences("DJDNO", Context.MODE_PRIVATE);
                    String remoteCancleTraceId = Remotepref.getString("djdJLBH", "");
                    if (remoteCancleTraceId.equals(""))
                        ed_old_saleId.setHint("原订金单交易号");
                    else
                        ed_old_saleId.setText(remoteCancleTraceId);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        btn_ensure_reprintTicket.setOnClickListener(this);
        tv_dialogtitle_name.setOnClickListener(this);
    }

    @Override
    public void showProgress() {
        dialog = new ProgressDialog(ReprintTicketActivity.this);
        dialog.setCancelable(false);
        dialog.setMessage("正在查询小票");
        dialog.show();
    }
    @Override
    public void hideLoading() {
        dialog.dismiss();
    }

    @Override
    public void showFailedError(String msg) {
        ToastUtils.show(msg);

    }

    @Override
    public void showSuccess(TicketInfo loadConfig) {
        List<String> list = TicketFormat.createTicketFormat(loadConfig, true,-1);
        for(int i = 0; i< GetConfigs.ticketPrintCount(); i++) {
            PrintManagerLib.initPrinter(ReprintTicketActivity.this, CommonUtils.getSystemModel())
                    .initDevice()
                    .printList(list);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ensure_reprintTicket: {
                String orderNo = ed_old_saleId.getText().toString().trim();

                if (CommonUtils.isNullOrEmpty(orderNo)){
                    ToastUtils.show("请输入单号");
                    break;
                }

                if (reFlag==0){
                    QueryOrderReq req = new QueryOrderReq();

                    if (orderNo.length() < 8){
                        req.setJlbh(orderNo);
                    }else {
                        req.setOrderNo(orderNo);
                    }

                    reprintTicketPresent.getReprintInfo(req);
                }else if (reFlag==1){
                    reprintTicketPresent.getReprintInfo1(reFlag, orderNo,this);
                }else if (reFlag==2){
                    reprintTicketPresent.getReprintInfo1(reFlag, orderNo,this);

                }else {
                    Toast.makeText(ReprintTicketActivity.this,"不能打印",Toast.LENGTH_SHORT).show();
                }
                finish();
            }
            break;
            case R.id.tv_dialog_title_name:
                  finish();
                break;

            default:
                break;
        }
    }
}
